package com.tys.algorithm.bak.code1;

public class Code20_MergeSort {

    //递归方式
    public static void mergeSort1(int[] arr) {
        if (arr == null || arr.length < 2) {
            return;
        }
        process(arr, 0, arr.length - 1);
    }

    // 请把arr[L..R]排有序
    // L...R N
    // T(N) = 2 * T(N / 2) + O(N)
    // O(N * logN)
    public static void process(int[] arr, int L, int R) {
        if (L == R) {
            return;
        }
        int mid = L + ((R - L) >> 1);
        process(arr, L, mid); //左有序
        process(arr, mid + 1, R); //右有序
        merge(arr, L, mid, R); //合并
    }

    //合并，arr 在 L...M有序，M+1...R有序
    public static void merge(int[] arr, int L, int M, int R) {
        int[] help = new int[R - L + 1]; //L...R有几个数
        int i = 0;
        int p1 = L; //左侧范围
        int p2 = M + 1; //右侧范围
        //左和右都不能越界
        while (p1 <= M && p2 <= R) {
            //谁小拷贝谁，并移动
            help[i++] = arr[p1] <= arr[p2] ? arr[p1++] : arr[p2++];
        }
        // 要么p1越界了，要么p2越界了
        //p1没越界
        while (p1 <= M) {
            help[i++] = arr[p1++];
        }
        //p2没越界
        while (p2 <= R) {
            help[i++] = arr[p2++];
        }
        //排好序的填入数组
        for (i = 0; i < help.length; i++) {
            arr[L + i] = help[i];
        }
    }

    //迭代方式：非递归
    public static void mergeSort2(int[] arr) {
        if (arr == null || arr.length < 2) {
            return;
        }
        int N = arr.length;
        int mergeSize = 1; //步长
        while (mergeSize < N) {
            int L = 0;  //当前左组的第一个位置
            while (L < N) {
                int M = L + mergeSize - 1;

                if (M >= N) { //左组不够时跳出
                    break;
                }
                int R = Math.min(M + mergeSize, N - 1); //右组
                merge(arr, L, M, R);
                L = R + 1; //下一个左组位置
            }
            //防止溢出，步长超过数组长度就溢出变为负值，整数最大值
            if (mergeSize > N / 2) {
                break;
            }
            mergeSize <<= 1; //步长*2
        }
    }

    public static void mergeSort3(int[] arr) {
        if (arr == null || arr.length < 2) {
            return;
        }
        int N = arr.length;
        int mergeSize = 1; //步长
        while (mergeSize < N) {
            int L = 0;  //当前左组的第一个位置
            while (L < N) {
                if (mergeSize >= N - L) { //左组不够时跳出
                    break;
                }
                int M = L + mergeSize - 1;
                int R = M + Math.min(mergeSize, N - M - 1); //右组
                merge(arr, L, M, R);
                L = R + 1; //下一个左组位置
            }
            //防止溢出，步长超过数组长度就溢出变为负值，整数最大值
            if (mergeSize > N / 2) {
                break;
            }
            mergeSize <<= 1; //步长*2
        }
    }

    public static int[] generateRandomArray(int maxSize, int maxValue) {
        int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random());
        }
        return arr;
    }

    public static int[] copyArray(int[] arr) {
        if (arr == null) {
            return null;
        }
        int[] res = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            res[i] = arr[i];
        }
        return res;
    }

    public static boolean isEqual(int[] arr1, int[] arr2) {
        if ((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null)) {
            return false;
        }
        if (arr1 == null && arr2 == null) {
            return true;
        }
        if (arr1.length != arr2.length) {
            return false;
        }
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] != arr2[i]) {
                return false;
            }
        }
        return true;
    }

    public static void printArray(int[] arr) {
        if (arr == null) {
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i] + " ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        int testTime = 500000;
        int maxSize = 100;
        int maxValue = 100;
        System.out.println("测试开始");
        for (int i = 0; i < testTime; i++) {
            int[] arr1 = generateRandomArray(maxSize, maxValue);
            int[] arr2 = copyArray(arr1);
            int[] arr3 = copyArray(arr1);
            mergeSort1(arr1);
            mergeSort2(arr2);
            mergeSort3(arr3);
            if (!isEqual(arr1, arr2)) {
                System.out.println("出错了");
                printArray(arr1);
                printArray(arr2);
                break;
            }
            if (!isEqual(arr2, arr3)) {
                System.out.println("出错了");
                printArray(arr2);
                printArray(arr3);
                break;
            }
        }
        System.out.println("测试结束");
    }
}
